﻿
//Author: 张亚涛
//createtime:2016-01-03 23:24:37
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "SmsCode" 表的方法;
    /// </summary>
    public class SmsCodeBase
    {
        const string SQL_Select = "select [ID],[Mobile],[Code],[SendTime] from [SmsCode] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回SmsCode表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<SmsCode> GetAllSmsCode()
        {
            List<SmsCode> smsCodeList = new List<SmsCode>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                SmsCode smsCode = new SmsCode(reader);
                smsCodeList.Add(smsCode);
            }
            reader.Close();
            return smsCodeList;
        }
        /// <summary>
        /// 根据SmsCode表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static SmsCode GetSmsCodeById(long id)
        {
            SmsCode smsCode = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                smsCode = new SmsCode(reader);
            }
            reader.Close();
            return smsCode;
        }
        /// <summary>
        /// 判断验证码是否正确
        /// </summary>
        /// <param name="Mobile">手机号</param>
        /// <param name="Code">验证码</param>
        /// <returns>True通过，Flase不通过</returns>
        public static bool Validate(string Mobile, string Code)
        {
            string SQL_Select = @"select TOP (1) Code from SmsCode where Mobile=@Mobile order by SendTime desc";
            SqlParameter[] parameters ={
                                         new SqlParameter("@Mobile",Mobile)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (ResultStr == Code)
            {
                return true;
            }
            return false;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据SmsCode表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteSmsCodeById(long id)
        {
            string sql = "delete from [SmsCode] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(SmsCode smsCode)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",smsCode.Id),
            new SqlParameter("@Mobile",smsCode.Mobile),
            new SqlParameter("@Code",smsCode.Code),
            new SqlParameter("@SendTime",smsCode.SendTime)    
            };
            return paras;
        }
        /// <summary>
        /// 根据SmsCode表的ID字段更新数据 
        /// </summary> 
        /// <param name="SmsCode">smsCode</param>
        /// <returns></returns>       
        public static int UpdateSmsCodeById(SmsCode smsCode)
        {
            string sql = "update [SmsCode] set [Mobile] = @Mobile,[Code] = @Code,[SendTime] = @SendTime where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(smsCode);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向SmsCode表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="smsCode">SmsCode</param>       
        /// <returns></returns>        
        public static int AddSmsCode(SmsCode smsCode)
        {
            string sql = "insert into [SmsCode]([Mobile],[Code],[SendTime])values(@Mobile,@Code,@SendTime)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(smsCode);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回SmsCode表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllSmsCode_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}